﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iCollect.Constants;
using iCollect.util;
using iCollect.CustomComponent;
using NationalInstruments.DAQmx;

namespace iCollect.CollectDetail
{
    public partial class Config1 : UserControl
    {
        private BaseDeviceSample ds = null;
        private DataTable mbDT;

        public Config1()
        {
            InitializeComponent();
        }

        public void loadDevice()
        {
            ds = UIUtil.getInstance().loadSample(ds, panel2, channelChanged);
            //ds.RaiseCustomEvent += channelChanged;
            loadTabs();
            this.tabInit();
            GlobalConstants.initChannelMap();

            cbNIChannels.Items.AddRange(DaqSystem.Local.GetPhysicalChannels(PhysicalChannelTypes.AI, PhysicalChannelAccess.External));
            
        }


        private void loadTabs()
        {
            if (GlobalConstants.type == "0")
            {
                if (this.tabControl1.TabPages["tabPage2"] == null)
                {
                    this.tabControl1.TabPages.Add(tabPage2);
                }
            }
            else
            {
                if (this.tabControl1.TabPages["tabPage2"] != null)
                {
                    this.tabControl1.TabPages.Remove(tabPage2);
                }
            }
        }

        private void initMBParam(string channelID)
        {
            try
            {
                mbDT = deviceparamTableAdapter.GetDataBy(channelID, GlobalConstants.currentDevice);

                if (mbDT.Rows.Count == 0)
                {
                    deviceparamTableAdapter.InsertQuery(channelID, GlobalConstants.DEFAULT_PORT,
                        GlobalConstants.DEFAULT_MB_SLAVE, GlobalConstants.channelMap[channelID].ToString(), 
                        GlobalConstants.DEFAULT_MB_INTERVAL, GlobalConstants.DEFAULT_MB_DATA_TYPE, 
                        GlobalConstants.currentDevice);
                    loadMBParam();
                }
                else
                {
                    DataRow row = mbDT.Rows[0];
                    tbMBChannelID.Text = row[0].ToString();
                    cbMBPort.Text = row[1].ToString();
                    numSlaveID.Value = int.Parse(row[2].ToString());
                    tbAddress.Text = row[3].ToString();
                    cbMBInterval.Text = row[4].ToString();
                    cbMBDataType.Text = row[5].ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Show the FolderBrowserDialog.
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                //textBoxPath.Text = folderBrowserDialog1.SelectedPath;
            }

        }

        private void tabInit()
        {
            ds.NIState();
        }

        private void tabChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                ds.NIState();
            }
            else if (tabControl1.SelectedIndex == 1)
            {
                ds.MBState();
            }

        }


        private void channelChanged(object sender, EventArgs e)
        {
            string channelID = ((Button)sender).Text;
            string channelType = channelID.Substring(0, 1);
            switch (channelType)
            {
                case "V":
                case "Z":
                    textBox3.Text = channelID;
                    //loadNIParam();
                    break;
                case "T":
                case "P":
                case "F":
                    tbMBChannelID.Text = channelID;
                    initMBParam(channelID);
                    break;
                default:
                    break;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (tbMBChannelID.Text == null || tbMBChannelID.Text == "" || tbAddress.Text == null || tbAddress.Text == "")
            {
                return;
            }
            try
            {
                int slaveID = (int)numSlaveID.Value;
                int interval = int.Parse(cbMBInterval.Text);
                deviceparamTableAdapter.UpdateQuery(tbMBChannelID.Text, cbMBPort.Text, slaveID,
                    tbAddress.Text, interval, cbMBDataType.Text, GlobalConstants.currentDevice,
                    tbMBChannelID.Text, GlobalConstants.currentDevice);

                GlobalConstants.channelMap[tbMBChannelID.Text] = tbAddress.Text;
                GlobalConstants.port = cbMBPort.Text;
                GlobalConstants.slaveID = (byte)this.numSlaveID.Value;
                GlobalConstants.mbInterval = interval;
                GlobalConstants.mbDataType = cbMBDataType.Text;

                UIUtil.getInstance().alert("保存成功。");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void loadMBParam()
        {
            cbMBPort.Text = GlobalConstants.port;
            numSlaveID.Value = GlobalConstants.slaveID;
            tbAddress.Text = GlobalConstants.channelMap[tbMBChannelID.Text].ToString();
            cbMBInterval.Text = GlobalConstants.mbInterval.ToString();
            cbMBDataType.Text = GlobalConstants.mbDataType;
        }

        private void deviceparamBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.deviceparamBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.cUPDataDataSet1);

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            loadMBParam();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            cbNIChannels.Items.Clear();
            cbNIChannels.Items.AddRange(DaqSystem.Local.GetPhysicalChannels(PhysicalChannelTypes.AI, PhysicalChannelAccess.External));
        }
    }
}
